#!/bin/bash
# - test_group

. $LKP_SRC/lib/reproduce-log.sh
. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/env.sh
. $LKP_SRC/lib/upload.sh

tmp_avocado="/tmp/avocado"

set_conf()
{
	[ -d "/etc/acocado" ] || log_cmd mkdir -p "/etc/avocado"
	local conf_file="/etc/avocado/avocado.conf"
	[ -d "$tmp_avocado/result" ] || log_cmd mkdir -p "$tmp_avocado/result"
	[ -d "$tmp_avocado/data" ] || log_cmd mkdir -p "$tmp_avocado/data"

	## Set avocado config file, specify those 4 important dir.
	## After test finished, upload $tmp_avocado to cluster.
	## $tmp_avocado includes all testting info.
	## Set test_dir to get all available tests by avocado list.
	cat <<EOT > $conf_file
[datadir.paths]
base_dir = $tmp_avocado
test_dir = $BENCHMARK_ROOT/avocado/examples/tests
data_dir = $tmp_avocado/data
logs_dir = $tmp_avocado/result
EOT
}

get_test()
{
	## avocado list will list all available tests
	log_cmd avocado list $BENCHMARK_ROOT/avocado/examples/tests >> /tmp/all_tests
	## There are two type tests: INSTRUMENTED and SIMPLE.
	## Don't test SIMPLE, all_tests will be splited.
	log_cmd sed -i "/^SIMPLE/d" /tmp/all_tests
	## Format all_test.
	## Before: INSTRUMENTED /lkp/benchmarks/avocado/examples/tests/abort.py:AbortTest.test
	## After: /lkp/benchmarks/avocado/examples/tests/abort.py:AbortTest.test
	gawk '{print $2 > "/tmp/all_test"}' /tmp/all_tests
}

split_test()
{
	## split all tests, each group has 20 tests.
	## e.g. split -l 20 all_test -d -a 2 group_
	## get group_00  group_01  group_02
	log_cmd split -l 20 /tmp/all_test -d -a 2 group_
}

run_test()
{
	while read line
	do
		pass_flag=0
		exit_output=$(avocado run $line --job-timeout 60  2>&1)
		pass_flag=$?
		if [ $pass_flag != 0 ] ; then
			if echo "$exit_output" | grep -q "FAIL: This test is supposed to fail" || echo "$exit_output" | grep -q "ERROR: This should end with ERROR" || echo "$exit_output" | grep -q "ERROR: Nasty-string-like-exception";then
				pass_flag=0
			fi
		fi
	done < $test_group
}

set_conf || die "Failed to set config file"
get_test || die "Failed to get all available tests"
split_test || die "Failed to split tests into test_group"
pass_flag=0
run_test
exit_value=$pass_flag

upload_files -t results $tmp_avocado/*

exit $exit_value
